<?
require("../../common/init.php");
require("author.php");

$gee=$customer_rule; // gee, 意为赶车时候喊的"驾", 此处用来表示次页面的主角对象
$title=ROOT_CUSTOMER_RULE;
$url='customer_rule';
$do=isset($_GET["do"])?clean($_GET["do"]):'view'; // 操作: 查看/编辑/删除
$action=isset($_POST["action"])?clean($_POST["action"]):''; // 数据库操作: CURD

$id=1; if (isset($_GET["id"])) $id = @$_GET["id"];

switch ($action){
    case "update":sql_update(); break;
    case "add": sql_insert(); break;
}

$where=array();
$where[]=array('name'=>'is_enabled','val'=>1);
//$rows=$gee->get_rows_advance(128,$where);
$rows=$gee->get_rows(10,'is_enabled', '1', 'level', 'asc');
//print_r($rows);

function add()
{
    global $url;
    $row = array();
    echo '<form enctype="multipart/form-data" method="post" action="'.$url.'.php">';
    add_box();
    echo '<input type="hidden" name="action" value="add"></form>';
}

function update($info)
{
    global $url;
    echo '<form method="post" action="'.$url.'.php">';
    show_box($info, true);
    echo '<input type="hidden" name="action" value="update">';
    echo '</form>';
}
function sql_insert()
{
    global $gee,$url, $lang;
    $arr=array(
        'name_'.$lang=>iso(@$_POST["name_".$lang]),
        'level'=>iso(@$_POST["level"]),
        'discount'=>iso(@$_POST["discount"]),
        'updated_time' => date('Y-m-d H:i:s', time()),
        'create_time' => date('Y-m-d H:i:s', time()),
        'updated_user' => $_COOKIE['userid'],
        'start_score' => iso(@$_POST['start']),
        'end_score' => iso(@$_POST['end']),
        'is_enabled' => 1
    );
    if($gee->insert($arr))
        echo '<script type="text/javascript">alert("添加成功! Success!");location="'.$url.'.php";</script>';
}
function show_info($info)
{
    global $url;
    show_box($info, false);
}
function sql_update()
{
    global $gee,$url,$lang;
    $data=$_POST;
    unset($data['action']);
    $ok=0;
    foreach($data as $key=>$val){
        $arr_name = explode('_', $key);
        if($arr_name[0] == 'name') $arr_name[0] = 'name_'.$lang;
        if($arr_name[0] == 'start') $arr_name[0] = 'start_score';
        if($arr_name[0] == 'end') $arr_name[0] = 'end_score';
        $id= $arr_name[count($arr_name)-1];
        $arr=array($arr_name[0]=>$val);
        if($gee->update($arr, 'id', $id ) ) $ok=1;
    }
    if($ok) yell(ROOT_UPDATE_SUCCESS,$url.".php");
}

function delete($id){
    global $gee,$url;
    if($gee->delete($id)){
        wheel("$url.php");
    }

}

// view 加载视图
require ROOTVIEW.'customer_rule.php';
